package br.com.hpedroni.gandroidengine.layers;

import br.com.hpedroni.gandroidengine.exceptions.ImageSizeException;
import br.com.hpedroni.gandroidengine.graphics.GameCanvas;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;

public class ParalaxBackGround extends Layer {

	private float copyX;
	private float copyY;
	private float speed = 0;

	private Bitmap original;
	private Bitmap copy;

	private final String TAG = ParalaxBackGround.class.getSimpleName();

	public ParalaxBackGround(Bitmap image, float x, float y) {
		setX(x);
		setY(y);
		this.original = image;
		Log.e(TAG, "Screen Width" + GameCanvas.SCREEN_WIDTH);
		try {
			setParalax();
		} catch (ImageSizeException e) {
			Log.e(TAG, e.getMessage());
		}
	}

	public ParalaxBackGround(Bitmap image) {
		setX(0);
		setY(0);
		this.original = image;
		try {
			setParalax();
		} catch (ImageSizeException e) {
			Log.e(TAG, e.getMessage());
		}

	}

	private void setParalax() throws ImageSizeException {
		if (this.original.getWidth() < GameCanvas.SCREEN_WIDTH) {
			throw new ImageSizeException();
		} else {
			this.copy = this.original;
			copyX = getX() + this.original.getWidth();
			copyY = getY();
		}
	}

	public void update(long gameTime) {
		move(speed, 0);
		copyX += speed;
		// movendo para a esquerda
		if (getX() + original.getWidth() < 0) {
			setX(copyX + copy.getWidth());
		}
		if (copyX + copy.getWidth() < 0) {
			copyX = getX() + original.getWidth();
		}
	}

	public void setPosition(float x, float y) {
		setX(x);
		setY(y);
	}

	public void setSpeed(float speed) {
		this.speed = speed;
	}

	@Override
	public void draw(Canvas canvas) {
		canvas.drawBitmap(this.original, getX(), getY(), null);
		canvas.drawBitmap(this.copy, copyX, copyY, null);
	}

}
